class ManufacturersController < ApplicationController
  before_action :set_manufacturer, only: [:show, :edit, :update, :destroy]

  # GET /manufacturers
  # GET /manufacturers.json
  def index
    @manufacturers = Manufacturer.all
  end

  # GET /manufacturers/1
  # GET /manufacturers/1.json
  def show
  end

  # GET /manufacturers/new
  def new
    @manufacturer = Manufacturer.new
  end

  # GET /manufacturers/1/edit
  def edit
  end

  # POST /manufacturers
  # POST /manufacturers.json
  def create
  	if params.has_key?(:drug_id)
		@drug = Drug.find(params[:drug_id])
		if params.has_key?(:exist) 
			@manufacturer = Manufacturer.where(manufacturer_params)
			@drug.manufacturers << @manufacturer
			respond_to do |format|
				format.html { redirect_to @drug, notice: 'Manufacturer was successfully created.' }
			end
		else	
			#@manufacturer = Manufacturer.new(manufacturer_params)
			@manufacturer = @drug.manufacturers.create(manufacturer_params)
	
			respond_to do |format|
			  if @manufacturer.save
				format.html { redirect_to @drug, notice: 'Manufacturer was successfully created.' }
				format.json { render :show, status: :created, location: @manufacturer }
			  else
				format.html { render :new }
				format.json { render json: @manufacturer.errors, status: :unprocessable_entity }
			  end
			end
		end
	else
		@manufacturer = Manufacturer.new(manufacturer_params)
		
		respond_to do |format|
		  if @manufacturer.save
			format.html { redirect_to @manufacturer, notice: 'Manufacturer was successfully created.' }
			format.json { render :show, status: :created, location: @manufacturer }
		  else
			format.html { render :new }
			format.json { render json: @manufacturer.errors, status: :unprocessable_entity }
		  end
		end
	end
  end

  # PATCH/PUT /manufacturers/1
  # PATCH/PUT /manufacturers/1.json
  def update
    respond_to do |format|
      if @manufacturer.update(manufacturer_params)
        format.html { redirect_to @drug, notice: 'Manufacturer was successfully updated.' }
        format.json { render :show, status: :ok, location: @manufacturer }
      else
        format.html { render :edit }
        format.json { render json: @manufacturer.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /manufacturers/1
  # DELETE /manufacturers/1.json
  def destroy
    @drug = Drug.find(params[:drug_id])
    @manufacturer.destroy
    respond_to do |format|
      format.html { redirect_to @drug, notice: 'Manufacturer was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_manufacturer
      @manufacturer = Manufacturer.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def manufacturer_params
      params.require(:manufacturer).permit(:name, :code, :website, :address, :email, :phone, :fax, :created_at, :updated_at, :created_by, :updated_by)
    end
end
